﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace VisionRoc.Tool
{
    internal class HttpHelper
    {
        public static string HttpPost(string url, string strJSON)
        {
            HttpWebRequest requestScore = null;
            Stream stream = null;
            StreamReader reader = null;
            HttpWebResponse responseSorce = null;
            try
            {
                requestScore = (HttpWebRequest)WebRequest.Create(url);
                byte[] data = Encoding.UTF8.GetBytes(strJSON.ToString());
                requestScore.Method = "Post";
                requestScore.ContentType = "application/json"; // "application/x-www-form-urlencoded";
                requestScore.ContentLength = data.Length;
                requestScore.KeepAlive = true;
                //requestScore.Headers.Add("X-Access-Token", TOKEN);

                stream = requestScore.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
                responseSorce = (HttpWebResponse)requestScore.GetResponse();
                reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.UTF8);
                string content = reader.ReadToEnd();
                return content;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                requestScore = null;
                if (responseSorce != null)
                {
                    responseSorce.Close();
                    responseSorce = null;
                }
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                    reader = null;
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }
            }
        }

        public static string HttpPut(string token, string url, string strJSON)
        {
            Stream stream = null;
            StreamReader streamReader = null;
            HttpWebResponse httpWebResponse = null;
            string result;
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                byte[] bytes = Encoding.UTF8.GetBytes(strJSON.ToString());
                httpWebRequest.Method = "Put";
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.ContentLength = (long)bytes.Length;
                httpWebRequest.KeepAlive = true;
                httpWebRequest.Headers.Add("X-Access-Token", token);
                stream = httpWebRequest.GetRequestStream();
                stream.Write(bytes, 0, bytes.Length);
                stream.Close();
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
                result = streamReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                    httpWebResponse = null;
                }
                if (streamReader != null)
                {
                    streamReader.Close();
                    streamReader.Dispose();
                    streamReader = null;
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }
            }
            return result;
        }

        public static string HttpPost(string token, string url, string strJSON)
        {
            HttpWebRequest requestScore = null;
            Stream stream = null;
            StreamReader reader = null;
            HttpWebResponse responseSorce = null;
            try
            {

                requestScore = (HttpWebRequest)WebRequest.Create(url);
                byte[] data = Encoding.UTF8.GetBytes(strJSON.ToString());
                requestScore.Method = "Post";
                requestScore.ContentType = "application/json"; // "application/x-www-form-urlencoded";
                requestScore.ContentLength = data.Length;
                requestScore.KeepAlive = true;
                requestScore.ServicePoint.Expect100Continue = false;
                requestScore.Headers.Add("X-Access-Token", token);

                stream = requestScore.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
                responseSorce = (HttpWebResponse)requestScore.GetResponse();
                reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.UTF8);
                string content = reader.ReadToEnd();
                return content;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                requestScore = null;
                if (responseSorce != null)
                {
                    responseSorce.Close();
                    responseSorce = null;
                }
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                    reader = null;
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }

            }

        }

        public static string HttpGet(string Url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }
    }
}
